﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dami;
public partial class Clients_Control_tintuc : System.Web.UI.UserControl
{
    private String strLiDiv = "<li> <a href='" + Constant.ROOT_PATH + "$link'>$text</a></li>";
    private String strNew = "<div class='box_new'><a href='$link'><img src='$src' width='100' alt='' class='$class' />$title</a><br><br>"
        + "$text<br/></div>";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {

            LoadDataRight();
            String strPage = "";
            try
            {
                strPage = Request.Params["Pro"].ToString().Trim();
            }
            catch
            {
                
            }
            lblContentTitle.Text = Constant.ContentTitle(strPage);
        }
    }

    private int Index(String str, String find)
    {
        int vt = -1;
        vt = str.IndexOf(find);
        return vt;
    }

    private void LoadDataRight()
    {

        //Hien thi mau tin tren trang chu
        int mautin = -1;
        int mota =230;
        if (!string.IsNullOrEmpty(Constant.ViewIndex))
        {
            try
            {
                mautin = Convert.ToInt32(Constant.ViewIndex);
                mota = Convert.ToInt32(Constant.Mota);
            }
            catch { }
        }
        mautin = mautin == -1 ? 4 : mautin;
        using (DamiWebDataContext db = new DamiWebDataContext())
        {
            var query = from p in db.tbl_News_Langs
                        where p.tbl_New.idNew == p.idNew
                        && p.idLang == 1 && p.tbl_New.isTindacbiet == false && p.tbl_New.isAuto == true
                        && p.tbl_New.isActive == 1 
                        orderby p.tbl_New.orderId descending, p.idNew descending
                        select new
                        {
                            p.titleNew,
                            p.idNew,
                            p.tbl_New.imageNew,
                            p.tbl_New.addedDate,
                            p.bodyNew,
                            p.introNew,
                            p.tbl_New.isAuto
                        };

            List<News> list = new List<News>();
            int i = 0;
            List<News> arr = new List<News>();
            News news =null;
            foreach (var c in query)
            {
                news = new News();
                news.idNew = c.idNew;
                String strFind = "";// c.introNew;
                int begin, end;
                if (c.isAuto == true)
                {
                    strFind = c.introNew;
                    begin = Index(strFind, "src=\"http://");
                    end = Index(strFind, ".jpg");
                    if (begin != -1 && end > begin + 3)
                    {
                        strFind = strFind.Substring(begin + 5, end - begin - 1);
                    }
                    else
                    {
                        strFind = "";
                    }
                }
                String strRece = c.introNew;
                begin = Index(strRece, "<a");
                end = Index(strRece, "</a>");
                if (end > begin)
                {
                    strRece = strRece.Substring(begin, end + 1);
                }
                else
                {
                    strRece = "";
                }
                if (!String.IsNullOrEmpty(c.imageNew))
                {
                    strFind = c.imageNew;
                }
                news.imageNew = strFind;
                if (String.IsNullOrEmpty(c.introNew))
                {
                    news.introNew = c.bodyNew;
                }
                else
                {
                    news.introNew = c.introNew;
                    if (!String.IsNullOrEmpty(strRece))
                    {
                        news.introNew = c.introNew.Replace(strRece, "");
                    }
                    news.introNew = Dami.clsFileUpLoad.DeHtml(news.introNew).Trim();
                }
                news.titleNew = c.titleNew;
                list.Add(news);
                i++;
                if (i == mautin)
                    break;
            }

            //tin tuc cong ty
            var cty = from p in db.tbl_News_Langs
                      where p.tbl_New.idNew == p.idNew
                      && p.idLang == 1 && p.tbl_New.isTindacbiet == false && p.tbl_New.isAuto == false
                      && p.tbl_New.isActive == 1 
                      orderby p.tbl_New.orderId descending,p.idNew descending
                      select new
                      {
                          p.titleNew,
                          p.idNew,
                          p.tbl_New.imageNew,
                          p.tbl_New.addedDate,
                          p.bodyNew,
                          p.introNew
                      };
            int dem = 0;
            String strNews = "";
            i = 0;
            int l = list.Count;
            foreach (var c in cty)
            {

                news = new News();
                news.idNew = c.idNew;
                news.imageNew = c.imageNew;
                arr.Add(news);
                String strlink = Constant.ROOT_PATH + String.Format("{0}/{1}/{2}", Constant.StrChiTietTinTuc,c.idNew, c.titleNew.ToString().
                                Replace(" ", "-").Replace(".", "").Replace("/", "_").Replace(":", "").Replace("\"", ""));

                String strPath = Constant.Dami == "" ? Constant.ROOT_PATH : Constant.Dami;
                //Hien thi mau tin

                String sImage = "";
                sImage = c.imageNew == null ? "" :  c.imageNew.ToString().Replace("~/", "");
                String strSrc = sImage;
                String strClass = sImage == "" ? "hideImg" : "";
                String strTitle = c.titleNew;
                String strText = c.introNew.ToString().Replace("<p>", "").Replace("</p>", ""); ;
                if (strText.Length > mota)
                {
                    strText = strText.Substring(0, mota) + " ...";
                }
                if (!clsFileUpLoad.CheckPath(strSrc))
                {
                    strSrc = "upload/news/no_image.jpg";
                }
                if (strTitle.Length > Constant.lenghtTitle)
                {
                    strTitle = strTitle.Substring(0, Constant.lenghtTitle) + " ...";
                }
                strSrc = strPath + strSrc;

                strNews = strNews + strNew.Replace("$link", strlink).Replace("$src", strSrc).
                    Replace("$class", strClass).Replace("$title", strTitle).Replace("$text", strText);

                arr.Add(list[i]);
                //tin tuc cap nhat tu dong
                strlink = Constant.ROOT_PATH + String.Format("{0}/{1}/{2}", Constant.StrChiTietTinTuc, list[i].idNew, list[i].titleNew.ToString().
                                Replace(" ", "-").Replace(".", "").Replace("/", "_").Replace(":", "").Replace("\"", ""));
                strSrc = list[i].imageNew;
                strClass = "";// "hideImg" + c.imageNew.ToString();
                strTitle = list[i].titleNew;
                strText = list[i].introNew.Replace("<p>", "").Replace("</p>", "");
                if (strText.Length > mota)
                {
                    strText = strText.Substring(0, mota) + " ...";
                }
                if (strTitle.Length > Constant.lenghtTitle)
                {
                    strTitle = strTitle.Substring(0, Constant.lenghtTitle) + " ...";
                }
                if (!clsFileUpLoad.CheckPath(strSrc))
                {
                    strSrc = "upload/news/no_image.jpg";
                }
                if (strSrc.IndexOf("http") == -1)
                {
                    strSrc = strPath + strSrc;
                }
                strNews = strNews + strNew.Replace("$link", strlink).Replace("$src", strSrc).
                    Replace("$class", strClass).Replace("$title", strTitle).Replace("$text", strText);
                dem++;
                i++;
                if (dem == l)
                    break;
            }
            lblNews.Text = strNews.Trim().Replace("<p>", "").Replace("</p>", "");
            LoadDataLeft(arr);
        }


    }

    private Boolean Find(List<News> list, int id) {
        Boolean bBool = false;
        foreach (News item in list) {
            if (item.idNew == id)
                return true;
        }
        return bBool;
    }
    private void LoadDataLeft(List<News> list)
    {
        int mautin = -1;
        try {
            mautin = Convert.ToInt32(Constant.View);
        }
        catch { }
        mautin = mautin == -1 ? 15 : mautin;
        using (DamiWebDataContext db = new DamiWebDataContext())
        {
            var query = from p in db.tbl_News_Langs
                        where p.tbl_New.idNew == p.idNew
                        && p.idLang == 1 && p.tbl_New.isTindacbiet == false
                        && p.tbl_New.isActive == 1 
                        orderby p.tbl_New.orderId descending, p.idNew descending
                        select new
                        {
                            p.titleNew,
                            p.idNew,
                            p.tbl_New.imageNew,
                            p.tbl_New.addedDate,
                            p.bodyNew,
                            p.introNew,
                            p.tbl_New.isAuto
                        };
            int i = 0;
            String url = "";
            foreach (var c in query)
            {
                if (!Find(list,c.idNew))
                {
                    try
                    {
                        if (!string.IsNullOrEmpty(c.ToString()))
                        {
                            String str = String.Format("{0}/{1}/{2}",Constant.StrChiTietTinTuc, c.idNew.ToString(), c.titleNew.ToString().
                                Replace(" ", "-").Replace(".", "").Replace("/", "_").Replace(":", "").Replace("\"", ""));
                            url = url + strLiDiv.Replace("$text", c.titleNew).Replace("$link", str);
                        }
                    }
                    catch { }
                    i++;
                    if (i >= mautin)
                        break;
                }
            }
            ltlTintuc.Text = url;
            if (string.IsNullOrEmpty(url))
            {
                lbl.Visible = false;
            }
            else
            {
                lbl.Visible = true;
            }
        }
    }
}